﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace QLThuChi.OtherForms
{
    #region Using
    using QLThuChi.Models;
    using QLThuChi.Repositories;
    using QLThuChi.Helpers;
    using QLThuChi.OtherForms;
    using DevComponents.DotNetBar;
    using QLThuChi.Reports;
    #endregion
    public partial class FrmThongKeDoanhThu : Office2007Form
    {
        // Hidden files are used to store ids 
        private DevComponents.DotNetBar.Controls.TextBoxX txtMaCongTrinh;

        public FrmThongKeDoanhThu()
        {
            InitializeComponent();
            InitHiddenFields();
        }

        /// <summary>
        /// Init hidden fields
        /// </summary>
        public void InitHiddenFields()
        {
            // Add a new textbox
            txtMaCongTrinh = new DevComponents.DotNetBar.Controls.TextBoxX
            {
                Name = "txtMaCongTrinh",
                Text = ""
            };
            txtMaCongTrinh.Visible = false;

        }

        private void FrmThongKeDoanhThu_Load(object sender, EventArgs e)
        {
        }

        private void btnChonCongTrinh_Click(object sender, EventArgs e)
        {
            FrmQuanLyCongTrinh frm = new FrmQuanLyCongTrinh(true);
            frm.Handler += GetCongTrinh;
            frm.ShowDialog();
        }

        public void GetCongTrinh(object sender, EventArgs e)
        {
            var eventType = (MyEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            txtMaCongTrinh.Text = comp[0];
            txtCongTrinh.Text = comp[1];
            LoadGiaDoanBelongToCongTrinh();
        }

        public void LoadGiaDoanBelongToCongTrinh()
        {
            var lstItem = CongTrinh_GiaiDoanRepository.SelectByMaCongTrinh(int.Parse(txtMaCongTrinh.Text));
            cbxGiaiDoan.Items.Clear();
            
            var all = new CongTrinh_GiaiDoan
            {
                MaGiaiDoan = 0
            };
            all.GiaiDoan = new GiaiDoan
            {
                TenGiaiDoan = "---Tất cả---"
            };
            cbxGiaiDoan.Items.Add(all);

            foreach (var item in lstItem)
            {
                cbxGiaiDoan.Items.Add(item);
            }

            cbxGiaiDoan.SelectedIndex = 0;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        public long CalChiVLXD(List<PhieuChi_VLXD> lstItem)
        {
            long sum = 0;
            for (int i = 0; i < lstItem.Count; i++)
            {
                sum += lstItem[i].ChiTietPhieux.Sum(item => item.SoLuong * item.DonGia).Value;
            }
            return sum;
        }

        private void btnBaoBieu_Click(object sender, EventArgs e)
        {
            if (cbxGiaiDoan.SelectedIndex != -1)
            {
                long tongthu = 0;
                long tongchi = 0;
                long tongdoanhthu = 0;
                var giaidoan = (CongTrinh_GiaiDoan)cbxGiaiDoan.SelectedItem;
                var lstItem = new List<DoanhThu>();
                // Select all of giai doan
                if (giaidoan.MaCongTrinhGiaiDoan == 0)
                {
                    var lstGiaiDoan = CongTrinh_GiaiDoanRepository.SelectByMaCongTrinh(int.Parse(txtMaCongTrinh.Text));
                    for (int i = 0; i < lstGiaiDoan.Count; i++)
                    {
                        var lstThu = PhieuThuRepository.SelectByMaCongTrinhGiaiDoan(lstGiaiDoan[i].MaCongTrinhGiaiDoan);
                        var lstChiLuong = PhieuChi_LuongNVRepository.SelectByMaCongTrinhGiaiDoan(lstGiaiDoan[i].MaCongTrinhGiaiDoan);
                        var lstChiVLXD = PhieuChi_VLXDRepository.SelectByMaCongTrinhGiaiDoan(lstGiaiDoan[i].MaCongTrinhGiaiDoan);
                        var thu = lstThu.Count > 0 ? lstThu.Sum(item => item.SoTien) : 0;
                        var chiluong = lstChiLuong.Count > 0 ? lstChiLuong.Sum(item => item.SoNgayCong * item.Luong) : 0;
                        var chiVLXD = CalChiVLXD(lstChiVLXD);

                        var doanhthu = (thu - (chiluong.Value + chiVLXD)).Value;

                        var dtgd = new DoanhThu
                        {
                            DoanhThuGiaiDoan = lstGiaiDoan[i].GiaiDoan,
                            Thu = thu.Value,
                            ChiLuong = chiluong.Value,
                            ChiVLXD = chiVLXD,
                            Loi = doanhthu >= 0 ? doanhthu : 0,
                            Lo = doanhthu < 0 ? -doanhthu : 0
                        };
                        lstItem.Add(dtgd);

                        tongthu += thu.Value;
                        tongchi += (chiluong + chiVLXD).Value;
                    }
                }
                else
                {
                    var lstThu = PhieuThuRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                    var lstChiLuong = PhieuChi_LuongNVRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                    var lstChiVLXD = PhieuChi_VLXDRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                    var thu = lstThu.Count > 0 ? lstThu.Sum(item => item.SoTien) : 0;
                    var chiluong = lstChiLuong.Count > 0 ? lstChiLuong.Sum(item => item.SoNgayCong * item.Luong) : 0;
                    var chiVLXD = CalChiVLXD(lstChiVLXD);

                    var doanhthu = (thu - (chiluong.Value + chiVLXD)).Value;

                    var dtgd = new DoanhThu
                    {
                        DoanhThuGiaiDoan = giaidoan.GiaiDoan,
                        Thu = thu.Value,
                        ChiLuong = chiluong.Value,
                        ChiVLXD = chiVLXD,
                        Loi = doanhthu >= 0 ? doanhthu : 0,
                        Lo = doanhthu < 0 ? -doanhthu : 0
                    };
                    lstItem.Add(dtgd);

                    tongthu += thu.Value;
                    tongchi += (chiluong + chiVLXD).Value;
                }

                tongdoanhthu = tongthu - tongchi;
                txtTongThu.Text = String.Format("{0:#,##0;Nothing}", tongthu);
                txtTongChi.Text = String.Format("{0:#,##0;Nothing}", tongchi);
                lblDoanhThu.Text = String.Format("{0:#,##0;Nothing}", tongdoanhthu);

                LapBaoBieu(lstItem);
            }
        }

        public void LapBaoBieu(List<DoanhThu> lstItem)
        {
            var dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("GiaiDoan");
            dt.Columns.Add("Thu");
            dt.Columns.Add("ChiLuong");
            dt.Columns.Add("ChiVLXD");
            dt.Columns.Add("Loi");
            dt.Columns.Add("Lo");

            for (int i = 0; i < lstItem.Count; i++)
            {
                dt.Rows.Add((i + 1).ToString(), lstItem[i].DoanhThuGiaiDoan.TenGiaiDoan, String.Format("{0:#,##0;Nothing}", lstItem[i].Thu), String.Format("{0:#,##0;Nothing}", lstItem[i].ChiLuong), String.Format("{0:#,##0;Nothing}", lstItem[i].ChiVLXD), String.Format("{0:#,##0;Nothing}", lstItem[i].Loi), String.Format("{0:#,##0;Nothing}", lstItem[i].Lo));
            }

            DataView objTimeSheetDV = new System.Data.DataView(dt);
            objTimeSheetDV.AllowNew = false;
            dtgvDataList.AutoGenerateColumns = false;
            dtgvDataList.DataSource = objTimeSheetDV;

            dtgvDataList.ClearSelection();
        }

        private void btnInBieu_Click(object sender, EventArgs e)
        {
            if (cbxGiaiDoan.SelectedIndex != -1)
            {
                var giaidoan = (CongTrinh_GiaiDoan)cbxGiaiDoan.SelectedItem;
                var lstItem = new List<DoanhThu>();
                // Select all of giai doan
                if (giaidoan.MaCongTrinhGiaiDoan == 0)
                {
                    var lstGiaiDoan = CongTrinh_GiaiDoanRepository.SelectByMaCongTrinh(int.Parse(txtMaCongTrinh.Text));
                    for (int i = 0; i < lstGiaiDoan.Count; i++)
                    {
                        var lstThu = PhieuThuRepository.SelectByMaCongTrinhGiaiDoan(lstGiaiDoan[i].MaCongTrinhGiaiDoan);
                        var lstChiLuong = PhieuChi_LuongNVRepository.SelectByMaCongTrinhGiaiDoan(lstGiaiDoan[i].MaCongTrinhGiaiDoan);
                        var lstChiVLXD = PhieuChi_VLXDRepository.SelectByMaCongTrinhGiaiDoan(lstGiaiDoan[i].MaCongTrinhGiaiDoan);
                        var thu = lstThu.Count > 0 ? lstThu.Sum(item => item.SoTien) : 0;
                        var chiluong = lstChiLuong.Count > 0 ? lstChiLuong.Sum(item => item.SoNgayCong * item.Luong) : 0;
                        var chiVLXD = CalChiVLXD(lstChiVLXD);

                        var doanhthu = (thu - (chiluong.Value + chiVLXD)).Value;

                        var dtgd = new DoanhThu
                        {
                            DoanhThuGiaiDoan = lstGiaiDoan[i].GiaiDoan,
                            Thu = thu.Value,
                            ChiLuong = chiluong.Value,
                            ChiVLXD = chiVLXD,
                            Loi = doanhthu >= 0 ? doanhthu : 0,
                            Lo = doanhthu < 0 ? -doanhthu : 0
                        };
                        lstItem.Add(dtgd);
                    }
                }
                else
                {
                    var lstThu = PhieuThuRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                    var lstChiLuong = PhieuChi_LuongNVRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                    var lstChiVLXD = PhieuChi_VLXDRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                    var thu = lstThu.Count > 0 ? lstThu.Sum(item => item.SoTien) : 0;
                    var chiluong = lstChiLuong.Count > 0 ? lstChiLuong.Sum(item => item.SoNgayCong * item.Luong) : 0;
                    var chiVLXD = CalChiVLXD(lstChiVLXD);

                    var doanhthu = (thu - (chiluong.Value + chiVLXD)).Value;

                    var dtgd = new DoanhThu
                    {
                        DoanhThuGiaiDoan = giaidoan.GiaiDoan,
                        Thu = thu.Value,
                        ChiLuong = chiluong.Value,
                        ChiVLXD = chiVLXD,
                        Loi = doanhthu >= 0 ? doanhthu : 0,
                        Lo = doanhthu < 0 ? -doanhthu : 0
                    };
                    lstItem.Add(dtgd);
                }

                FrmPrintDoanhThu frm = new FrmPrintDoanhThu(lstItem, txtCongTrinh.Text);
                frm.ShowDialog();
            }
        }
    }
}
